//package com.hefei.garden.config;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.hefei.garden.pojo.Result;
//import lombok.SneakyThrows;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//import reactor.netty.ByteBufFlux;
//
///**
// * @author: devil
// * @Date: 2022/9/6 22
// * @Description: 全局拦截器
// */
//@Component
//public class AuthGlobalFilter implements GlobalFilter, Ordered {
//
//    private AntPathMatcher antPathMatcher = new AntPathMatcher();
//
//    private static final String TOKEN = "token";
//
//    @SneakyThrows
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//        String path = request.getURI().getPath();
//        Result<Object> result = null;
//        // 说明请求是走网关的
//        if (antPathMatcher.match("/hefei-south-railway-station/**", path)) {
//            String token = request.getHeaders().getFirst(TOKEN);
//            if (StringUtils.isBlank(token)) {
//                result = Result.error();
//            } else {
//                result = Result.success();
//            }
//        } else {
//            //内部服务接口，不允许外部直接访问
//            if (antPathMatcher.match("/**/inner/**", path)) {
//                result = Result.error();
//            }
//        }
//        ServerHttpResponse response = exchange.getResponse();
//        ObjectMapper jackson = new ObjectMapper();
//        String resultStr = jackson.writeValueAsString(result);
//        Flux<Flux<DataBuffer>> just = Flux.just(ByteBufFlux.just(response.bufferFactory().wrap(resultStr.getBytes())));
//        return response.writeAndFlushWith(just);
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//
//}
